package com.sgb.kjwl.view.ui.adapter.self.goods;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemChoiceVipPriceGoodsBinding;
import com.sgb.kjwl.databinding.ItemVipPriceMangeBinding;
import com.sgb.kjwl.model.entity.self.VipPriceMangeListEntity;
import com.sgb.kjwl.utils.AbScreenUtils;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.adapter.map.BaseRecycleAdapter;
import com.sgb.kjwl.view.ui.adapter.map.ItemViewHolder;

import java.util.ArrayList;

public class ChoiceVipPriceGoodsAdpater extends BaseRecycleAdapter<VipPriceMangeListEntity> {
    public int choice_position = -1;
    public ChoiceVipPriceGoodsAdpater(Context context) {
        super(context, R.layout.item_choice_vip_price_goods, new ArrayList<>());
    }

    @Override
    public void convert(ItemViewHolder holder, VipPriceMangeListEntity vipPriceMangeListEntity) {
        holder.getmBinding().setVariable(BR.model,vipPriceMangeListEntity);
        if(holder.getmBinding() instanceof ItemChoiceVipPriceGoodsBinding){

            String pinpai = "/";
            if(!TextUtils.isEmpty(vipPriceMangeListEntity.getBrandName())){
                pinpai = vipPriceMangeListEntity.getBrandName();
                if(!TextUtils.isEmpty(vipPriceMangeListEntity.getPreciseStorageName())){
                    pinpai = pinpai+" | "+vipPriceMangeListEntity.getPreciseStorageName();
                }
            }else if(!TextUtils.isEmpty(vipPriceMangeListEntity.getPreciseStorageName())){
                pinpai = vipPriceMangeListEntity.getPreciseStorageName();
            }

            if(!TextUtils.isEmpty(pinpai)){
                ((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).tvPinpai.setText(pinpai);
            }

            if(vipPriceMangeListEntity.getPriceMax()==0&&vipPriceMangeListEntity.getPriceMin()==0){
                ((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).tvPrice.setText(PointUtils.price(vipPriceMangeListEntity.getPrice())+"/"+vipPriceMangeListEntity.getUnit());
            }else{
                ((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).tvPrice.setText(PointUtils.price(vipPriceMangeListEntity.getPriceMin())+"-"+PointUtils.price(vipPriceMangeListEntity.getPriceMax())+"/"+vipPriceMangeListEntity.getUnit());
            }

            if(vipPriceMangeListEntity.getImageList()!=null&&vipPriceMangeListEntity.getImageList().length>0){
                RequestOptions options = new RequestOptions().transform(new CenterCrop(),new RoundedCorners(AbScreenUtils.dp2px(context,4)))
                        .placeholder(R.mipmap.image_order_list_defuntimage)
                        .error(R.mipmap.image_order_list_defuntimage);
//                RequestOptions options = new RequestOptions().bitmapTransform().placeholder(context.getResources().getDrawable(R.mipmap.image_order_list_defuntimage));

                Glide.with(context).load(vipPriceMangeListEntity.getImageList()[0]).apply(options).into(((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).image);
            }

            ((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).checkBox.setImageResource(vipPriceMangeListEntity.isIschoice()?R.drawable.icon_selected:R.drawable.icon_unselecte);


            ((ItemChoiceVipPriceGoodsBinding) holder.getmBinding()).checkBox.setFocusable(false);//禁止点击checkbox
            holder.getmBinding().getRoot().setOnClickListener(view -> {
                Log.e("position",holder.getPosition()+"");

                if(vipPriceMangeListEntity.getIsMemberPrice()!=1) {
                    for (int i = 0; i < datas.size(); i++) {
                        if (i == holder.getPosition() - 1) {
                            choice_position = i;
                            datas.get(i).setIschoice(true);
                        } else {
                            datas.get(i).setIschoice(false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }


}
